{% extends "layout.html" %}
{% block content %}
    <div class="x-body layui-anim layui-anim-up">
        <form class="layui-form" lay-filter="dataItem">
			<table class="layui-table">
				<tbody>
                    <tr>
						<th><span class="x-red">*</span>标签名</th>
						<td colspan="3">
                            <input type="text" id="label" name="label" required="required" lay-verify="required"
                            autocomplete="off" class="layui-input">
                        </td>
                    </tr>
					<!-- <tr>
						<th><span class="x-red">*</span>slave ID</th>
						<td>
							<input type="text" id="slave_id" name="slave_id" required="required" lay-verify="slave_id"
                            autocomplete="off" class="layui-input">
						</td>
						<th><span class="x-red">*</span>寄存器类型</th>
						<td>
							<select id="register_type" name="register_type" lay-filter="register_type">
                                <option value="1">线圈状态</option>
                                <option value="2">输入状态</option>
                                <option value="4">保持寄存器</option>
                                <option value="3" selected>输入寄存器</option>
                            </select>
						</td>
					</tr> -->
					<tr>
						<th>索引</th>
						<td>
							<input type="text" id="data_index" name="data_index" required="required|number" lay-verify="data_index"
                            autocomplete="off" class="layui-input" value="0">
						</td>
						<th>地址</th>
						<td>
							<input type="text" id="address" name="address" required="required|number" lay-verify="address"
                            autocomplete="off" class="layui-input" value="0">
						</td>
					</tr>
					<tr>
						<th>数据类型</th>
						<td>
							<select id="data_type" name="data_type" lay-filter="data_type">
                                <option value="1">双字节整数(小端)</option>
                                <option value="2" selected>双字节整数(大端)</option>
                                <option value="3">四字节浮点数(小端)</option>
                                <option value="4">四字节浮点数(小端2)</option>
                                <option value="5">四字节浮点数(大端)</option>
                                <option value="6">四字节浮点数(大端2)</option>
                                <option value="7">遥信</option>
                                <!-- <option value="6">双字节二进制</option> -->
                            </select>
						</td>
						<th>系数</th>
						<td>
							<input type="text" id="coefficient" name="coefficient" required="required|number" lay-verify="coefficient"
                            autocomplete="off" class="layui-input" value="1">
						</td>
					</tr>
					<tr>
						<th>取反</th>
						<td>
							<!-- <input type="text" id="negation" name="negation" required="number" lay-verify="negation"
                            autocomplete="off" class="layui-input" value="0"> -->
                            <input type="checkbox" name="negation" lay-skin="switch" lay-text="取反|正常">
						</td>
						<th>BCD</th>
						<td>
							<!-- <input type="text" id="BCD" name="BCD" required="number" lay-verify="BCD"
                            autocomplete="off" class="layui-input" value="1"> -->
                            <input type="checkbox" name="BCD" lay-skin="switch" lay-text="BCD|普通" disabled>
						</td>
					</tr>
					<tr>
						<th>ABS</th>
						<td>
                            <input type="checkbox" name="ABS" lay-skin="switch" lay-text="ABS|正常">
						</td>
						<th>基值</th>
						<td>
							<input type="text" id="base_value" name="base_value" required="number" lay-verify="base_value"
                            autocomplete="off" class="layui-input" value="0.0">
						</td>
					</tr>
					<tr>
						<th>介绍</th>
						<td colspan="3">
							<input type="text" id="introduction" name="introduction" required="number" lay-verify=""
                            autocomplete="off" class="layui-input" value="">
						</td>
					</tr>
					<!-- <tr>
						<th>数据表</th>
						<td>
							<input type="text" id="data_table" name="data_table" required="" lay-verify=""
                            autocomplete="off" class="layui-input">
						</td>
						<th>字段</th>
						<td>
							<input type="text" id="field" name="field" required="" lay-verify=""
                            autocomplete="off" class="layui-input">
						</td>
					</tr> -->
					<tr>
						<th>当前值</th>
						<td colspan="3">
							<input type="text" id="value" name="value" required="" lay-verify=""
                            autocomplete="off" class="layui-input" value="1.0">
						</td>
					</tr>
					<tr>
						<td colspan="2" style="text-align: center;">
                            <button id="confirmBtn" class="layui-btn" lay-filter="save" lay-submit="">
                                增加
                            </button>
                        </td>
						<td colspan="2" style="text-align: center;">
							<button  type="reset" class="layui-btn">
                                重置
                            </button>
						</td>
					</tr>
                </tbody>
            </table>
        </form>
    </div>
    <script>
        var dataItem_id=GetQueryString('dataItem_id');
        var channel_id=GetQueryString('channel_id');
        layui.use(['form','layer'], function(){
            $ = layui.jquery;
            var form = layui.form
            ,layer = layui.layer;
            //自定义验证规则
            form.verify({
            });

            //监听提交
            form.on('submit(save)', function(data){
                data.field['channel_id']=channel_id;
                if(dataItem_id!=null)
                    data.field['dataItem_id']=dataItem_id;
                $.ajax({
                    url:'checkDataItem',
                    type:'POST',
                    dataType:'json',
                    data:data.field,
                    success:function(check_result){
                        if(check_result.code==0){
                            $.ajax({url:'saveDataItem',type:'POST',dataType:'json',data:data.field,success:function(data){
                                // console.log(data);
                                if(data.code==0){
                                    // layer.msg('操作成功!');
                                    layer.alert("操作成功", {icon: 6},function () {
                                        parent.location.reload();
                                        // 获得frame索引
                                        var index = parent.layer.getFrameIndex(window.name);
                                        //关闭当前frame
                                        parent.layer.close(index);
                                    });
                                }
                            }});
                        }else{
                            layer.open({
                                title: '存在错误！'
                                ,content: check_result.msg+"<br>请修改数据地址！<br>或者修改采集通道地址/数量"
                            });
                        }
                    }
                });
                return false;
            });
            layer.ready(function(){
                if(dataItem_id!=null){
                    data={'dataItem_id':dataItem_id};
                    $('#confirmBtn').html('保存');
                    $.ajax(
                    {
                        url:'readDataItem'
                        ,type:'POST'
                        ,dataType:'json'
                        ,data:data
                        ,success:function(data){
                            //设置内容
                            if(data.code==0){
                                dataItem=data.data[0];
                                channel_id=dataItem.channel_id;
                                form.val("dataItem",{
                                    label:dataItem.label,
                                    slave_id:dataItem.slave_id,
                                    register_type:dataItem.register_type,
                                    data_index:dataItem.data_index,
                                    address:dataItem.address,
                                    data_type:dataItem.data_type,
                                    coefficient:dataItem.coefficient,
                                    base_value:dataItem.base_value,
                                    introduction:dataItem.introduction,
                                    data_table:dataItem.data_table,
                                    field:dataItem.field,
                                    value:dataItem.value
                                });
                                if(dataItem.negation==1){
                                    form.val("dataItem",{
                                        negation:"on"
                                    });
                                }
                                if(dataItem.BCD==1){
                                    form.val("dataItem",{
                                        BCD:"on"
                                    });
                                }
                                if(dataItem.ABS==1){
                                    form.val("dataItem",{
                                        ABS:"on"
                                    });
                                }
                                // channel_id=data.data[0].channel_id;
                                // $('#label').val(data.data[0].label);
                                // $('#slave_id').val(data.data[0].slave_id);
                                // $('#register_type').val(data.data[0].register_type);
                                layui.form.render();
                            }
                        }
                    });
                }
            });
        });
    </script>
{% endblock %}